package com.fowo.api.controller.shipment.wfs;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.StockOut;
import com.fowo.api.model.platformshipping.PlatformshippingCancelOrderAction;
import com.fowo.api.model.platformshipping.PlatformshippingVo;
import com.fowo.api.service.impl.PlatformshippingServiceImpl;
import com.fowo.api.service.impl.StockOutServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

@Component
public class platformshippingCancelOrderAction implements PlatformshippingCancelOrderAction {
    @Resource
    private PlatformshippingServiceImpl platformshippingService;
    @Resource
    private StockOutServiceImpl stockOutService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void run(Long id) throws Exception {
            handle(id);
    }
    //作废
    private void handle(Long id) throws Exception {
        PlatformshippingVo voById = platformshippingService.getVoById(id);
        Assert.isTrue(voById.getShippingstatus().equals("2"),"违规操作");
        voById.setShippingstatus("3");
        platformshippingService.update(voById);
//        platformshippingService.releaseInventory(List.of(voById.getId()));
        //查询出库单撤销
        StockOut one = stockOutService.getOne(Wrappers.<StockOut>query().lambda()
                .eq(StockOut::getRemark, voById.getShipmentCode())
                .eq(StockOut::getStatus,"CS02")
        );
        stockOutService.annul(one.getId());
    }
}
